package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.TblDeviceActions;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 插卡取电面板
 */
@Data
public class ChaKaQuDianMianBan_0FAA8106 extends BaseFrame {

    private String switchState;
    private String cardState;
    private String laterTime;


    public ChaKaQuDianMianBan_0FAA8106(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据
        this.setCardState(getCardState(data));

        //此属性需要从数据库查询，根据结果进行赋值
        this.laterTime = null;

        //数据库操作 begin
        SelectDeviceMapper testMapper = SpringUtil.getBean(SelectDeviceMapper.class);
        List<TblDeviceActions> list = testMapper.getDevice2(this.getDeviceMac(), this.getOdIndex(), this.getDeviceType(), this.getProductType());
        if (CollectionUtils.isEmpty(list)) {
            laterTime = "02";
        } else {
            if (list.size() <= 1 && list.get(0) == null) {
                laterTime = "02";
            }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == null) {
                    laterTime = "02";
                }else {
                    if ("laterTime".equals(list.get(i).getActionsKey())) {
                        laterTime = list.get(i).getActionsValue();
                    }
                }

            }
        }

        if (laterTime == null) {
            laterTime = "02";
        }
        //数据库操作 end

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("laterTime", laterTime);
        actions.put("cardState", cardState);
        this.setActions(actions);

        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);
    }

    /**
     * @Description:获取插卡取电状态
     * @param:
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/2 10:08
     */
    private static String getCardState(String data) {
        return StringUtils.substring(data, data.length() - 2, data.length());
    }


}
